package com.agricultural.machine.messaging;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
 * 消息类 - 用于农机间通信
 */
public class Message {
    private final String senderId;         // 发送者ID
    private final String receiverId;       // 接收者ID，"broadcast"表示广播消息
    private final String type;             // 消息类型
    private final String content;          // 消息内容
    private final LocalDateTime timestamp; // 消息时间戳
    private final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
    
    public Message(String senderId, String receiverId, String type, String content) {
        this.senderId = senderId;
        this.receiverId = receiverId;
        this.type = type;
        this.content = content;
        this.timestamp = LocalDateTime.now();
    }
    
    public String getSenderId() {
        return senderId;
    }
    
    public String getReceiverId() {
        return receiverId;
    }
    
    public String getType() {
        return type;
    }
    
    public String getContent() {
        return content;
    }
    
    public LocalDateTime getTimestamp() {
        return timestamp;
    }
    
    public boolean isBroadcast() {
        return "broadcast".equals(receiverId);
    }
    
    @Override
    public String toString() {
        return String.format("[%s] %s -> %s [%s]: %s",
                timestamp.format(formatter),
                senderId,
                isBroadcast() ? "全部" : receiverId,
                type,
                content);
    }
} 